#include <math.h>
#include "ProjectHeader.h"
#include "Camera.h"

void Camera::SetFOV(GLdouble minFOV, GLfloat thresholdSpeed, GLdouble maxFOV, GLfloat highSpeed)
{
	_minFOV = minFOV;
	_thresholdSpeed = thresholdSpeed;
	_maxFOV = maxFOV;
	_highSpeed = highSpeed;
}

void Camera::SetScreenParams(vertex2f screenSize, GLdouble nearClip, GLdouble farClip)
{
	_aspect = screenSize.u / screenSize.v;
	_nearClip = nearClip;
	_farClip = farClip;
}

void Camera::SetZPos(GLfloat zPos)
{
	_zPos = zPos;
}

void Camera::SetOrthoView(GLdouble NearClip, GLdouble FarClip)
{
	_orthoNearClip = NearClip;
	_orthoFarClip = FarClip;
}
void Camera::SetOrthoActive()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 1.0, 0.0, 1.0, _orthoNearClip, _orthoFarClip);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void Camera::UpdateTarget_NoSpeed(vertex2f target)
{
	vertex2f delta;
	delta.u = target.u - _oldTarget.u;
	delta.v = target.v - _oldTarget.v;
	GLfloat speed = sqrt((delta.u * delta.u) + (delta.v * delta.v));
	UpdateTarget(target, speed);
}

void Camera::UpdateTarget(vertex2f target, GLfloat speed)
{
	//glTranslatef(target.u, target.v, _zPos);
	
	GLdouble FOV;
	if(speed < _thresholdSpeed){FOV = _minFOV;}
	else if(speed > _highSpeed){FOV = _maxFOV;}
	else 
	{
		GLdouble deltaFOV = _maxFOV - _minFOV;
		GLdouble deltaSpeed = _highSpeed - _thresholdSpeed;
		GLdouble degreesPerSpeed = deltaFOV / deltaSpeed;
		
		FOV = degreesPerSpeed * (speed - _thresholdSpeed) + _minFOV;
	}

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(FOV, _aspect, _nearClip, _farClip);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(target.u, target.v, -_zPos, target.u, target.v, 0.0l, 0.0l, 1.0l, 0.0l);
}